/**
 * Copyright (C) 2010 Telecom Digital
 */
package cc.telecomdigital.tdstock.activity.chart;

import java.util.List;

import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import cc.mango.android.chartdata.ChartDataHelper;
import cc.mango.android.chartutil.ChartViewFactory;
import cc.mango.android.dto.StkDaily;
import cc.mango.android.dto.StkIntraday;
import cc.mango.common.FinalKey;
import cc.telecomdigital.tdstock.BitmapResizer;
import cc.telecomdigital.tdstock.ITDLApplication;
import cc.telecomdigital.tdstock.R;
import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;

public class StkChartActivity extends NvChart
{

	private static String	dateType	= FinalKey.INTRADAY_DATETYPE_TODAY;

	private static boolean	isRsi	= false;							//false : vol    true :  Rsi
	private static boolean	isCs		= false;							//false : line   true :  Vs
	private static boolean	isSma	= false;							//false :not ma   true :   ma
	private static boolean	isDaily	= false;							//false :1day 1week     true :   over 1month


	
	/** Called when the activity is first created. */	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		setContentView(R.layout.nv_chart);

		/*this.bm = BitmapFactory.decodeResource(this.getResources(),
				R.drawable.item_chart_price_column);//by zhengan
*/
		this.bm =  BitmapResizer.decodeImage(this.getResources(),
				R.drawable.item_chart_price_column, 79);  //its size : 79 X 26
		
		/*this.circle = BitmapFactory.decodeResource(this.getResources(),
				R.drawable.nvchart_circle);*/
		
		this.circle  =   BitmapResizer.decodeImage(this.getResources(),
				R.drawable.nvchart_circle, 14);  //its size : 14 X 14

		/*sunFlower = BitmapFactory.decodeResource(this.getResources(),
				R.drawable.nvchart_sunflower);*/
		sunFlower =  BitmapResizer.decodeImage(this.getResources(),
				R.drawable.nvchart_sunflower, 35);  //its size : 35 X 35

		getRes();

		findViews();

		theItemCode = this.getIntent().getStringExtra(FinalKey.ITEMCODE);

		//by zhengan
		if (theItemCode == null)
		{
			TDStockLog.d("Print_Info", "===== the theItemCode is null");
			theItemCode = "1";
		}

		updateTitle();

		setListeners();

		chartDataHelper = new ChartDataHelper(theItemCode, this
				.getApplicationContext(), getActivationCode());//by zhengan

		reviseChartTextSize();

	}

	private void updateTitle()
	{
		String stockName = ((ITDLApplication) this.getApplicationContext())
				.getCurrentStockName();

		if (stockName != null)
		{
			stockName = stockName.replaceAll("[\n\r]", "").replaceAll(
					"(<br>)|(<BR>)", "");
		}
		_stkname.setText(stockName == null ? "" : stockName);
	}

	@Override
	protected void setImageButtonListeners()
	{

		ImageButton.OnClickListener btLineListener = new Button.OnClickListener()
		{
			public void onClick(View v)
			{

				isCs = false;

				changeAllImageButtonStatus();

				drawAboveChart(getDateType());

			}
		};

		ImageButton.OnClickListener btCsListener = new Button.OnClickListener()
		{
			public void onClick(View v)
			{

				isCs = true;

				changeAllImageButtonStatus();

				drawAboveChart(getDateType());

			}
		};

		ImageButton.OnClickListener btSmaListener = new Button.OnClickListener()
		{
			public void onClick(View v)
			{

				isSma = !isSma;

				changeAllImageButtonStatus();

				drawAboveChart(getDateType());
			}
		};

		ImageButton.OnClickListener btVolListener = new Button.OnClickListener()
		{
			public void onClick(View v)
			{

				isRsi = false;
				changeAllImageButtonStatus();
				drawDownChart(getDateType());

			}
		};

		ImageButton.OnClickListener btRsiListener = new Button.OnClickListener()
		{
			public void onClick(View v)
			{

				isRsi = true;

				changeAllImageButtonStatus();

				drawDownChart(getDateType());

			}
		};

		_btline.setOnClickListener(btLineListener);
		_btcandlestick.setOnClickListener(btCsListener);
		_btsma.setOnClickListener(btSmaListener);
		_btvol.setOnClickListener(btVolListener);
		_btrsi.setOnClickListener(btRsiListener);
	}

	public void updateStkIntradayInfoView(StkIntraday stk)
	{

		String dateTime = stk.getDtyyyymmdd().substring(4, 6) + "月"
				+ stk.getDtyyyymmdd().substring(6, 8)
				+ "日" //haibo, 2011-06-23,  remove the space to adapt to small screen
				+ stk.getTime().substring(0, 2) + ":"
				+ stk.getTime().substring(2, 4);

		updateTextView(_tv_labels[1], dateTime);

		String vol = getStringVol(stk.getVol());

		updateTextView(_tv_labels[2], _res_strings[0] + vol);

		String high = getStringPrice(stk.getDayhigh());

		updateTextView(_tv_labels[3], _res_strings[1] + high);

		String low = getStringPrice(stk.getDaylow());

		updateTextView(_tv_labels[4], _res_strings[2] + low);

		String open = getStringPrice(stk.getOpen());

		updateTextView(_tv_labels[5], _res_strings[3] + open);
	}

	public void updateStkDailyInfoView(StkDaily stk)
	{

		String dateTime = stk.getDtyyyymmdd().substring(2, 4) + "年"
				+ stk.getDtyyyymmdd().substring(4, 6) + "月"
				+ stk.getDtyyyymmdd().substring(6, 8) + "日";

		TDStockLog.d(TAG, "" + stk.toString());

		updateTextView(_tv_labels[1], dateTime);

		if (isRsi)
		{

			if (isEqual(stk.getRsi(), -1))
			{

				updateTextView(_tv_labels[2], _res_strings[4]
						+ _res_strings[5]);
			}
			else
			{

				String rsi = getStringRsi(stk.getRsi());

				updateTextView(_tv_labels[2], _res_strings[4] + rsi);

			}

		}
		else
		{

			String vol = getStringVol(stk.getVol());

			updateTextView(_tv_labels[2], _res_strings[0] + vol);

		}

		String high = getStringPrice(stk.getHigh());

		updateTextView(_tv_labels[3], _res_strings[1] + high);

		String low = getStringPrice(stk.getLow());

		updateTextView(_tv_labels[4], _res_strings[2] + low);

		String open = getStringPrice(stk.getOpen());

		updateTextView(_tv_labels[5], _res_strings[3] + open);
	}

	protected void setDateType(String dateType)
	{

		StkChartActivity.dateType = dateType;

	}

	public String getDateType()
	{

		return dateType;

	}

	protected void handleImageButtonStatus()
	{

		if (FinalKey.INTRADAY_DATETYPE_TODAY.equals(getDateType())
				|| FinalKey.INTRADAY_DATETYPE_WEEK.equals(getDateType()))
		{

			isDaily = false;

			isRsi = false;//再次切换为Daily数据时，回到!isRsi，即不是Rsi图

			//  isSma = false;//再次切换为Daily数据时，isSma不改变，与先前状态一致

			changeAllImageButtonStatus();

		}
		else
		{

			isDaily = true;

			changeAllImageButtonStatus();
		}

	}

	protected void changeAllImageButtonStatus()
	{

		setImageButtonStatus(_btrsi, !isRsi && isDaily);
		setImageButtonStatus(_btvol, isRsi && isDaily);

		setImageButtonStatus(_btcandlestick, !isCs);
		setImageButtonStatus(_btline, isCs);

		setImageButtonStatus(_btsma, isDaily);

	}

	protected View getDownView(List datalist, String datetype)
	{

		View view;

		if (isRsi && isDaily)
		{

			view = ChartViewFactory.getStkRsiChartView(
					StkChartActivity.this, datalist);

		}
		else
		{

			view = ChartViewFactory.getStkVolChartView(
					StkChartActivity.this, datalist);
		}

		return view;

	}

	protected View getUpView(List datalist, String dateType)
	{

		View view = null;

		if (isCs)
		{

			if (isSma && isDaily)
			{

				view = ChartViewFactory.getCandlestickSmaChartView(
						StkChartActivity.this, datalist, dateType);
			}
			else
			{

				view = ChartViewFactory.getCandlestickChartView(
						StkChartActivity.this, datalist, dateType);
			}

		}
		else
		{

			if (isSma && isDaily)
			{

				view = ChartViewFactory.getStkLineSmaChartView(
						StkChartActivity.this, datalist, dateType);
			}
			else
			{

				view = ChartViewFactory.getStkLineChartView(
						StkChartActivity.this, datalist, dateType);
			}

		}

		return view;
	}

	protected void getUpAndDownView(List dataList, String dateType)
	{

		_aboveView = getUpView(dataList, dateType);

		_underView = getDownView(dataList, dateType);

	}
	
	
	protected void onResume() {
		super.onResume();
		
		//((ITDLApplication)(getApplicationContext())).SetAsActiveActivity(this);
		
	}

}